package com.moto.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moto.entity.Car;
import com.moto.entity.CarBrand;
import com.moto.entity.Category;
import com.moto.entity.User;
import com.moto.entityDto.*;
import com.moto.interceptor.BaseContext;
import com.moto.result.Result;
import com.moto.service.ICarBrandService;
import com.moto.service.ICarService;
import com.moto.service.ICategoryService;
import com.moto.utils.PageResult;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 车辆表 前端控制器
 * </p>
 *
 * @author hyf
 * @since 2023-08-28
 */
@RestController
@RequestMapping("/admin/car")
@Slf4j
@CrossOrigin(origins = "*")
@Api(tags = "摩托车相关接口")
public class CarController {

    @Autowired
    private ICarService carService;
    @Autowired
    private ICategoryService categoryService;

    /**
     * 新增车辆
     */
    @PostMapping("/addCar")
    @ApiOperation("新增摩托车")
    @Transactional
    public Result<Car> addCar(@RequestBody CarCatDto carCatDto){
        System.out.println("时间为："+ LocalDateTime.now());
        log.info("新增摩托车：{}",carCatDto);
        carService.addCar(carCatDto);
        return Result.success();
    }

    /**
     * 摩托车分页查询
     */
    @GetMapping("finaAllCar")
    @ApiOperation("摩托车分页查询")
    public Result<PageResult> findAllCar(CarPageDto carPageDto){
        log.info("摩托车分页查询:{}",carPageDto);
        PageResult pageResult = carService.findAllCar(carPageDto);
        if (pageResult.getTotal()==0){
            return Result.error("没有找到你所要查找的车辆");
        }
        return Result.success(pageResult);
    }


    /**
     * 根据摩托id查找信息
     */
    @GetMapping("/findCarById")
    @ApiOperation("根据摩托车id查找信息并返回")
    public Result<Car> findCarById(int id){
        log.info("id：{}",id);
        LambdaQueryWrapper<Car> lqw = new LambdaQueryWrapper<Car>();
        lqw.eq(Car::getId,id);
        Car car = carService.getOne(lqw);
        return Result.success(car);
    }


    /**
     * 修改摩托车信息
     */
    @PutMapping("/updateCar")
    @ApiOperation("修改摩托车信息")
    public Result updateCar(@RequestBody CarUpdateDto carUpdateDto){
        log.info("修改摩托车所接受的参数：{}",carUpdateDto);
        Integer id = Math.toIntExact(BaseContext.getCurrentId());
        carUpdateDto.setUpdateId(id);
        carUpdateDto.setUpdateDate(LocalDateTime.now());
        log.info("修改摩托车信息为：{}",carUpdateDto);
        carService.updateCarById(carUpdateDto);
        return Result.success();
    }

    /**
     * 进行摩托车的假删除
     */
    @DeleteMapping("/deleteCar/{id}")
    @ApiOperation("摩托车假删除")
    public Result carDelete(@PathVariable int id){
        carService.removeById(id);
        return Result.success();
    }

    /**
     * 返回所有类型
     */
    @GetMapping("findCategory")
    @ApiOperation("摩托车类型查询")
    public Result<List<Category>> findCategory(){
        List<Category> list = categoryService.list();
        return Result.success(list);
    }





}
